const xss = require("xss");
const { exec } = require("../db/mysql");

const blogTypeList = () => {
  let sql = `select * from content_type`;
  return exec(sql);
};
const addBlogType = (blogData = {}) => {
  const content_type_name = xss(blogData.content_type_name);
  const sql = `insert into content_type (content_type_name) 
    values('${content_type_name}');`;

  return exec(sql).then((insertData) => {
    if (insertData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const updateBlogType = (id, blogData = {}) => {
  const content_type_name = xss(blogData.content_type_name);

  const sql = `update content_type set content_type_name='${content_type_name}' where content_type_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const delBlogType = (id) => {
  const Selsql = `select blog.content_type_id from blog where content_type_id=${id}`;
  return exec(Selsql).then((rows) => {
    if (rows[0] === undefined) {
       const sql = `delete from content_type where content_type_id=${id}`;
       return exec(sql).then((delData) => {
         if (delData.affectedRows > 0) {
           return true;
         }
         return false;
       });
    } else {
      return {
        code: -1,
        msg: "无法删除该类型",
      };
    }
  });
};
//--------------------------------------------------------------------------
const blogList = (isadmin) => {
  let sql = `select blog.*, user.user_name,content_type.content_type_name
   from blog,user,content_type where blog.user_id=user.user_id and 
   blog.content_type_id=content_type.content_type_id and blog.isadmin='${isadmin}'`;
  return exec(sql);
};
const getDetail = (id) => {
  const sql = `select * from blog where blog_id='${id}'`;
  return exec(sql).then((rows) => {
    return rows[0];
  });
};
const addBlog = (blogData = {}) => {
  let blog_title = blogData.blog_title;
  let blog_content = blogData.blog_content;
  let content_type_id = blogData.content_type_id;
  let blog_time = blogData.blog_time;
  let user_id = blogData.user_id;
  let isadmin = blogData.isadmin;

  const sql = `insert into blog 
  (blog_title,blog_content,content_type_id,blog_time,user_id,isadmin) 
    values('${blog_title}','${blog_content}',${content_type_id},'${blog_time}','${user_id}','${isadmin}');`;

  return exec(sql).then((insertData) => {
    return {
      id: insertData.insertId,
    };
  });
};
const updateBlog = (id, blogData = {}) => {
  let blog_title = blogData.blog_title;
  let blog_content = blogData.blog_content;
  let content_type_id = blogData.content_type_id;
  let blog_time = blogData.blog_time;

  const sql = `update blog set blog_title='${blog_title}', 
  blog_content='${blog_content}', content_type_id='${content_type_id}', 
  blog_time='${blog_time}' where blog_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const updateBlogTop = (id, blogData = {}) => {
  let is_top = blogData.is_top;

  const sql = `update blog set is_top='${is_top}' where blog_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const delBlog = (id) => {
  const sql = `delete from blog where blog_id=${id}`;
  return exec(sql).then((delData) => {
    if (delData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};

module.exports = {
  blogTypeList,
  addBlogType,
  updateBlogType,
  delBlogType,
  //-------------
  blogList,
  getDetail,
  addBlog,
  updateBlog,
  updateBlogTop,
  delBlog,
};
